package com.robot.common.utils;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 时间分割工具类
 *
 */
public class DateSplitUtil {

    private DateSplitUtil() {
        throw new IllegalStateException("请通过静态方法调用！");
    }

    /**
     * 传入两个时间范围，返回这两个时间范围内的所有日期，并保存在一个集合中
     *
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     */
    public static List<String> findEveryDay(String beginTime, String endTime)
            throws Exception {
        //创建一个放所有日期的集合
        List<String> dates = new ArrayList();

        //创建时间解析对象规定解析格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //将传入的时间解析成Date类型,相当于格式化
        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);

        //将格式化后的第一天添加进集合
        dates.add(sdf.format(dBegin));

        //使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();

        //传入起始时间将此日历设置为起始日历
        calBegin.setTime(dBegin);

        //判断结束日期前一天是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {

            //根据日历的规则:月份中的每一天，为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);

            //得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
            //如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }


    /**
     * 获取两个日期之间的所有月份 (年月)
     *
     * @param startTime
     * @param endTime
     * @return：list
     */
    public static List<String> getMonthBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {

                // 把日期添加到集合
                list.add(sdf.format(startDate));

                // 设置日期
                calendar.setTime(startDate);

                //把月数增加 1
                calendar.add(Calendar.MONTH, 1);

                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取两个日期之间的所有年
     *
     * @param startTime
     * @param endTime
     * @return：list
     */
    public static List<String> getYearBetweenDate(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        // 声明保存日期集合
        List<String> list = new ArrayList<>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {

                // 把日期添加到集合
                list.add(sdf.format(startDate));

                // 设置日期
                calendar.setTime(startDate);

                //把年数增加 1
                calendar.add(Calendar.YEAR, 1);

                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    /**
     * 获取时间段内周数列表
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<String> getWeekBetweenDates(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        ArrayList<String> result = new ArrayList<>();
        try {
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();

            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            min.setTime(startDate);
            max.setTime(endDate);

            Calendar curr = min;
            String weekNum = "";
            while (curr.compareTo(max) <= 0) {
                int weekOfYear = curr.get(Calendar.YEAR);
                int weekNo = curr.get(Calendar.WEEK_OF_YEAR);
                //Calendar获取月份需要+1
                int nowMonth = curr.get(Calendar.MONTH) +1;
                //正常12月份肯定都是大月份的周数如5352515049如果周数是1而月份是12,则表示日期被计算在了下一年年份需要+1
                if(weekNo==1 && nowMonth==12){
                    weekOfYear = weekOfYear + 1;
                }
                //1月份肯定都是小周数如1,2,3,4,5如果周数是52或者53而月份是1 则表示日期被计算在了上一年,年份需要-1
                if((weekNo==53 || weekNo==52) && nowMonth==1){
                        weekOfYear = weekOfYear - 1;
                }
                String temp = String.valueOf(weekOfYear) +(curr.get(Calendar.WEEK_OF_YEAR));
                if (!temp.equals(weekNum)) {
                    weekNum = temp;
                    result.add(weekNum);
                }
                curr.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }



    /**
     * 获取今天的开始时间
     *
     * @return String
     */
    public static String getTodayStart(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date) + " 00:00:00";
    }

    /**
     * 获取第二天的开始时间
     *
     * @return String
     */
    public static String getNextDayStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }


    /**
     * 获取今天的结束时间
     *
     * @return String
     */
    public static String getTodayEnd(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date) + " 23:59:59";
    }


    /**
     * 获取本月开始日期
     *
     * @return String
     **/
    public static String getMonthStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }



    /**
     * 获取第二个月的开始日期
     *
     * @return String
     **/
    public static String getNextMonthStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, 1);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
    }



    /**
     * 获取本月最后一天
     *
     * @return String
     **/
    public static String getMonthEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
    }


    /**
     * 获取本年的第一天
     *
     * @return String
     **/
    public static String getYearStart(Date date) {
        return new SimpleDateFormat("yyyy").format(date) + "-01-01 00:00:00";
    }


    /**
     * 获取第二年的第一天
     *
     * @return String
     **/
    public static String getNextYearStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.YEAR,1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy").format(time) + "-01-01 00:00:00";
    }


    /**
     * 获取本年的最后一天
     *
     * @return String
     **/
    public static String getYearEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast) + " 23:59:59";
    }



}




